﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace Ycjw.PipeLine.Map.Basic.Results
{

    public class ResultWindowWithChart : ResultWindow
    {
        /// <summary>
        /// DataGridView控件
        /// </summary>
        protected DataGridView m_DataGridView;
        /// <summary>
        /// UIChart控件
        /// </summary>
        protected UIChart m_UIChart;
        /// <summary>
        /// UIChart的Option属性
        /// </summary>
        protected UIOption m_UIOption;
        /// <summary>
        /// Chart类型，默认为Bar
        /// </summary>
        protected UIChartType m_ChartType = UIChartType.Bar;
        /// <summary>
        /// 默认的Chart标题
        /// </summary>
        protected string m_ChartTitle = "统计结果";
        /// <summary>
        /// 是否显示表格DataGridView，默认显示
        /// </summary>
        protected bool ShowDataGridView = true;
        /// <summary>
        /// 设置Chart图的标题
        /// </summary>
        protected virtual string ChartTitle
        {
            get { return m_ChartTitle; }
            set { m_ChartTitle = value; }
        }
        /// <summary>
        /// 图标类型
        /// </summary>
        public enum UIChartType
        {
            /// <summary>
            /// 柱状图
            /// </summary>
            Bar,
            /// <summary>
            /// 折线图
            /// </summary>
            Line,
            /// <summary>
            /// 饼图
            /// </summary>
            Pie,
            /// <summary>
            /// 扩展后的柱状图
            /// </summary>
            BarEx,
            /// <summary>
            /// 跑道图
            /// </summary>
            Doughnut
        }
        public ResultWindowWithChart() : base()
        {
            CreateExportImageAndExcelButton();
            base.uiSymbolButtonExportToExcel.Click += UiSymbolButtonExportToExcel_Click;
            base.uiSymbolButtonExportToImage.Click += UiSymbolButtonExportToImage_Click;
            CreateChart();
            InitializeComponent();
            SetColors();
        }
        /// <summary>
        /// 导出图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UiSymbolButtonExportToImage_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog
            {
                DefaultExt = "jpg",
                Filter = "图片文件(*.jpg)|*.jpg",
                RestoreDirectory = true,
                Title = "图片文件保存路径"
            };

            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                Screenshots(this.m_UIChart,saveFileDialog.FileName);
            }
        }
        /// <summary>
        /// 对控件截图
        /// </summary>
        /// <param name="ctrl"></param>
        /// <param name="filepath"></param>
        protected void Screenshots(Control ctrl,string filepath)
        {
            Bitmap bmp = new Bitmap(ctrl.Width, ctrl.Height);
            ctrl.DrawToBitmap(bmp, ctrl.ClientRectangle);
            bmp.Save(filepath);
        }
        /// <summary>
        /// 导出Excel
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UiSymbolButtonExportToExcel_Click(object sender, EventArgs e)
        {
            if (m_DataGridView == null) return;
            var errMsg = Commons.ExportDgvToExcel.ExportExcel(string.Empty, this.m_DataGridView, "雅黑", 14);
            if (!string.IsNullOrEmpty(errMsg))
            {
                if (errMsg == Commons.ExportDgvToExcel.CANCELRESULTSTRING) return;
                else  Sunny.UI.UINotifier.Show(errMsg, Sunny.UI.UINotifierType.ERROR, "错误", false, 1000);
            }
            else
            {
                Sunny.UI.UINotifier.Show("导出成功", Sunny.UI.UINotifierType.OK, "消息", false, 1000);
            }
        }
        /// <summary>
        /// 大小改变时修改Chart的大小为总高度的2/3
        /// </summary>
        /// <param name="e"></param>
        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            if (m_UIChart == null) return;

            this.m_UIChart.Height = 2* this.Height / 3;
        }
        /// <summary>
        /// 根据实际需要展示的图类型Pie/Bar创建Chart控件
        /// </summary>
        protected virtual void CreateChart()
        {
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // uiChart
            // 
            this.m_UIChart.Dock = System.Windows.Forms.DockStyle.Fill;
            this.m_UIChart.FillColor = System.Drawing.Color.FromArgb(((int)(((byte)(244)))), ((int)(((byte)(244)))), ((int)(((byte)(244)))));
            this.m_UIChart.Font = new System.Drawing.Font("微软雅黑", 12F);
            this.m_UIChart.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(54)))), ((int)(((byte)(54)))), ((int)(((byte)(54)))));
            this.m_UIChart.Location = new System.Drawing.Point(0, 0);
            this.m_UIChart.MinimumSize = new System.Drawing.Size(1, 1);
            this.m_UIChart.Name = "uiChart";
            this.m_UIChart.Size = new System.Drawing.Size(800, 450);
            this.m_UIChart.TabIndex = 0;
            this.m_UIChart.Text = "uiChart";
            // 
            // ResultWindowWithChart
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.ClientSize = new System.Drawing.Size(800, 450);
            this.Name = "ResultWindowWithChart";
            this.ResumeLayout(false);

        }
        protected virtual void AddOptionSeries(DataTable dt)
        {
        }
        protected virtual void AddXAxis(DataTable dt)
        {

        }
        /// <summary>
        /// 将Chart控件添加到界面
        /// </summary>
        protected virtual void AddChart()
        {
            m_UIChart.RadiusSides = UICornerRadiusSides.None;
            m_UIChart.RectSides = ToolStripStatusLabelBorderSides.None;
            this.Controls.Add(this.m_UIChart);
            if (this.ShowDataGridView)
                m_UIChart.Dock = DockStyle.Top;
            else
                m_UIChart.Dock = DockStyle.Fill;
        }
        /// <summary>
        /// 创建DataGridView控件并绑定数据源到DataTable
        /// </summary>
        /// <param name="dt"></param>
        protected virtual void CreateDataGridView(DataTable dt)
        {
            if (m_DataGridView == null)
            {
                m_DataGridView = new Sunny.UI.UIDataGridView
                {
                    Dock = DockStyle.Fill,
                    AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill,
                    SelectionMode = DataGridViewSelectionMode.FullRowSelect,
                    DefaultCellStyle = new DataGridViewCellStyle()
                    {
                        ForeColor = Color.Black
                    },
                    AllowUserToAddRows = false,
                    AllowUserToResizeRows = false,
                    BorderStyle = BorderStyle.None,
                    CellBorderStyle = DataGridViewCellBorderStyle.None,
                    ReadOnly = true,
                    RowHeadersVisible = false
                };
                this.Controls.Add(m_DataGridView);
            }

            m_DataGridView.DataSource = dt;
        }
        /// <summary>
        /// 设置图例，默认右下角
        /// </summary>
        protected virtual void SetLegend()
        {
            //设置Legend
            m_UIOption.Legend = new UILegend
            {
                Orient = UIOrient.Horizontal,
                Top = UITopAlignment.Bottom,
                Left = UILeftAlignment.Right
            };
        }
        /// <summary>
        /// 根据
        /// </summary>
        /// <param name="dt"></param>
        protected virtual void SetLegendItem(DataTable dt)
        {
        }
        /// <summary>
        /// 设置X轴显示的文字
        /// </summary>
        /// <returns></returns>
        protected virtual string SetXAxisName()
        {
            return "值";
        }
        /// <summary>
        /// 设置Y轴显示的文字
        /// </summary>
        /// <returns></returns>
        protected virtual string SetYAxisName()
        {
            return "系列";
        }
        /// <summary>
        /// 设置Chart的Option的扩展信息
        /// </summary>
        protected virtual void SetOptionEx()
        {
        }
        /// <summary>
        /// 根据传入的数据显示Chart图和表格
        /// </summary>
        /// <param name="data"></param>
        public override void ShowData(object data)
        {
            if (!(data is DataTable))
            {
                Sunny.UI.UIMessageTip.ShowWarning("数据错误，未能显示图表");
                return;
            }
            var dt = data as DataTable;

            if (ShowDataGridView)
            {
                this.CreateDataGridView(dt);
            }
            AddChart();
            m_UIOption.Title = new UITitle
            {
                Text = ChartTitle
            };

            AddOptionSeries(dt);
            AddXAxis(dt);
            SetLegend();
            SetLegendItem(dt);            

            SetOptionEx();

            this.m_UIChart.SetOption(m_UIOption);
        }
        protected static List<Color>  m_Colors = new List<Color>();
        private static void SetColors(int count = 100)
        {
            if (m_Colors.Count > 0)
                return;
            long tick = DateTime.Now.Ticks;
            Random ran = new Random((int)(tick & 0xffffffffL) | (int)(tick >> 32));
            Random ran2 = new Random((int)(tick & 0xffffffffL) | (int)(tick >> 32));
            for (int i = 0; i < count; i++)
            {
                int R = ran.Next(255);
                int G = ran2.Next(255);
                int B = 222;
                B = (R + G > 400) ? R + G - 400 : B;//0 : 380 - R - G;
                B = (B > 255) ? 255 : B;

                m_Colors.Add(System.Drawing.Color.FromArgb(R, G, B));
            }
        } 
    }
}
